package com.test21_常用类;

import org.junit.Test;

public class Demo01_String {

    @Test
    public void test1() {
        {
            // StringBuffer
            StringBuffer buffer = new StringBuffer();
            buffer.append("沐言科技：").append("www.yootk.com").append("\n");
            change(buffer);     // 引用传递
            System.out.println(buffer);
        }
        System.out.println("==============================");
        {
            // String内容不可变
            String message = "沐言科技：www.yootk.com\n";
            change(message);
            System.out.println(message);
        }
    }

    public static void change(StringBuffer temp) {
        temp.append("李兴华编程训练营：").append("yootk.ke.qq.com");
    }

    public static void change(String temp) {
        temp += "李兴华编程训练营：yootk.ke.qq.com";
    }

    @Test
    public void test2() {
        StringBuffer buffer = new StringBuffer(30);
        // 字符串追加
        buffer.append("李兴华编程训练营：").append("yootk.ke.qq.com");
        // 字符串反转
//        buffer.reverse();
//        System.out.println(buffer);
        // 字符串插入
        buffer.insert(0, "【沐言优拓】");
        System.out.println(buffer);
        // 字符串替换
        buffer.replace(6, 14, "学习小课堂");
        System.out.println(buffer);
        // 字符串删除
        buffer.delete(6, 11);
        System.out.println(buffer);
    }

    @Test
    public void test3() {
        // StringBuilder，JDK1.5引入
        StringBuilder builder = new StringBuilder(30);
        builder.append("yootk.ke.qq.com").insert(0, "李兴华编程训练营：");
        System.out.println(builder);
    }

    /**
     * StringBuffer和StringBuilder的区别：
     * 1、StringBuffer属于线程安全的处理过程
     * 2、StringBuilder属于非线程安全的处理过程
     */
    @Test
    public void test4() throws Exception {
        {
            StringBuilder builder = new StringBuilder();
            for (int x = 0; x < 5; x++) {
                new Thread(() -> {
                    builder.append(Thread.currentThread().getName()).append("\n");
                }, "线程对象-" + x).start();
            }
            Thread.sleep(1000);     // 等待线程执行完毕
            System.out.println(builder);
        }
        System.out.println("===================================");
        {
            StringBuffer buffer = new StringBuffer();
            for (int x = 0; x < 5; x++) {
                new Thread(() -> {
                    buffer.append(Thread.currentThread().getName()).append("\n");
                }, "线程对象-" + x).start();
            }
            Thread.sleep(1000);     // 等待线程执行完毕
            System.out.println(buffer);
        }
    }

    /**
     * CharSequence接口
     * String、StringBuffer、StringBuilder都实现了CharSequence接口
     */
    @Test
    public void test5() {
        print("沐言优拓：www.yootk.com");
        print(new StringBuffer("沐言优拓：www.yootk.com"));
        print(new StringBuilder("沐言优拓：www.yootk.com"));
    }

    public static void print(CharSequence sequence) {
        System.out.println("【PRINT】" + sequence);
        System.out.println("【截取字符串】" + sequence.subSequence(5, 18));
    }

    @Test
    public void test6() {
        String str = "沐言优拓：www.yootk.com";
        // String转StringBuffer
        StringBuffer buffer = new StringBuffer(str);
        System.out.println(buffer.delete(0, 5));
        // StringBuffer转String
        String message = buffer.toString();
        // String message = new String(buffer);
        System.out.println(message.toUpperCase());
    }
}
